import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;


public class TowerControlFrame extends JFrame implements ActionListener {

	private JMenuBar menuBar;
	private JMenu sessionMenu;	//this has all the options for managing the
								//current session
	private JMenu towerMenu;	//controls aspects of the tower
	private JMenu airportMenu;	//controls aspects of the airport

	//session Menu Items
	private JMenuItem logoutItem;	//i	f the user wants to logout
	private JMenuItem exitItem;	//if the user wants to shutdown

	private Session currentSession;	//this is the user managing the current instance of the TowerControlFrame
	/**
	 * The constructor for the TowerControlFrame; it initializes the UI
	 *
	 */
	public TowerControlFrame(Session session) {
		currentSession = session;

		initUI();
	}

	/**
	 * Initializes the UI components of TowerControlFrame
	 */
	private void initUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		//initialize the menubar
		menuBar = new JMenuBar();

		sessionMenu = new JMenu("Session");
		towerMenu = new JMenu("Tower");
		airportMenu = new JMenu("Airport");

		logoutItem = new JMenuItem("Logout");
		logoutItem.addActionListener(this);
		exitItem = new JMenuItem("Exit TowerControl");
		exitItem.addActionListener(this);

		sessionMenu.add(logoutItem);
		sessionMenu.add(exitItem);

		menuBar.add(sessionMenu);
		//set the menubar
		setJMenuBar(menuBar);

		//depending on what kind of position the user is,
		//load the corresponding panel
		String position = currentSession.getUserPosition();

		JPanel positionPanel = null;

		if ("admin".equals(currentSession.getUser().getUsername())) {
			position = "User Administration";
 			 positionPanel = new AdminPanel();
 		} else if ("Supervisor".equals(position)) {
			positionPanel = new SupervisorPanel();
		} else if ("Traffic Manager".equals(position)) {
			positionPanel = new TrafficManagerPanel();
		} else if ("Flight Data/Clearance Delivery".equals(position)) {
			positionPanel = new FlightDataPanel();
		} else if ("Ground Control".equals(position)) {
			positionPanel = new GroundControlPanel();
		} else if ("Local Control".equals(position)) {
			positionPanel = new LocalControlPanel();
 		} else if ("Helicopter Control".equals(position)) {
 			positionPanel = new HelicopterControlPanel();
 		} else {
 			//test if it's the admin account
 			JOptionPane.showMessageDialog(this, "TowerControlFrame: Invalid position type specified!");
 			SessionManager.getInstance().logout(currentSession);
 			dispose();
 			//error
 			return;
 		}

		if (positionPanel != null) {
			getContentPane().add(positionPanel);
		}

		setSize(800,600);
		setTitle("Tower Control - " + position);
	}

	/**
	 * Handles all the menu actions n TowerControlFrame
	 * @param e Represents what action happened
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == logoutItem) {

			int result = JOptionPane.showConfirmDialog(this,
					"Are you sure you want to logout?",
					"Tower Control",
					JOptionPane.YES_NO_OPTION);


			if (result == JOptionPane.YES_OPTION) {
				dispose();
				SessionManager.getInstance().logout(currentSession);
			}

		} else if (e.getSource() == exitItem) {
			int result = JOptionPane.showConfirmDialog(this,
			"Are you sure you want to quit TowerControl?",
			"Tower Control",
			JOptionPane.YES_NO_OPTION);

			if (result == JOptionPane.YES_OPTION) {
				dispose();
				SessionManager.getInstance().exit();
			}


		}
	}
}
